﻿#include "riggerview.h"
#include <QDebug>
#include <QScrollBar>
#include <QWheelEvent>
#include <QEvent>
#include <QPointF>

RiggerView::RiggerView(QWidget* parent)
	: QGraphicsView(parent)
{
	//    p_RightBtnMenu = new QMenu(this);
	//    m_MenuActionList.append(p_RightBtnMenu->addAction(QIcon(":/icons/zoom_out.png"), QString("放大"), this, &RiggerView::viewZoomOut));
	//    m_MenuActionList.append(p_RightBtnMenu->addAction(QIcon(":/icons/zoom_in.png"), QString("缩小"), this, &RiggerView::viewZoomIn));
	//    m_MenuActionList.append(p_RightBtnMenu->addAction(QString("重置"), this, &RiggerView::viewZoomReset));
}

//bool RiggerView::eventFilter(QObject *obj, QEvent *event)
//{
//    QGraphicsView::eventFilter(obj, event);
//}

void RiggerView::wheelEvent(QWheelEvent* event)
{
	if (event->modifiers() == Qt::ControlModifier)
	{
		QPointF cursorPos = event->position();//event->pos();
		QPointF scenePos = mapFromScene(event->position());

		QPoint angle = event->angleDelta() / 8;
		int numDegrees = angle.y();
		if (numDegrees > 0)
		{
			viewZoomOut();
		}
		else if (numDegrees < 0)
		{
			viewZoomIn();
		}

		QPointF viewPoint = m_Transform.map(scenePos);
		if (horizontalScrollBar()->isVisible())
		{
			horizontalScrollBar()->setValue(int(viewPoint.x() - cursorPos.x()));
		}

		if (verticalScrollBar()->isVisible())
		{
			verticalScrollBar()->setValue(int(viewPoint.y() - cursorPos.y()));
		}
	}
}

void RiggerView::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::RightButton)
	{

	}
	QGraphicsView::mousePressEvent(event);
}

void RiggerView::keyPressEvent(QKeyEvent* event)
{
	QGraphicsView::keyPressEvent(event);
}

//void RiggerView::contextMenuEvent(QContextMenuEvent *event)
//{
//    p_RightBtnMenu->exec(QCursor::pos());
//    QGraphicsView::contextMenuEvent(event);
//}

void RiggerView::viewZoomOut()
{
	if (m_Transform.m11() * 1.1 > 2.0)
		return;

	m_Transform.scale(1.1, 1.1);
	setTransform(m_Transform);
	//    qDebug()<<"zoom in"<<m_Transform.m11();
}

void RiggerView::viewZoomIn()
{
	if (m_Transform.m11() / 1.1 < 0.5)
		return;

	m_Transform.scale(1 / 1.1, 1 / 1.1);
	setTransform(m_Transform);
	//    qDebug()<<"zoom out"<<m_Transform.m11();
}

void RiggerView::viewZoomReset()
{
	m_Transform.reset();
	setTransform(m_Transform);
	//    qDebug()<<"zoom reset"<<m_Transform.m11();
}
